package com.youngjun.user.service.impl;

import com.youngjun.user.mapper.UserRoleMapper;
import com.youngjun.user.pojo.domain.UserRole;
import com.youngjun.user.pojo.dto.UserRoleDTO;
import com.youngjun.user.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserRoleServiceImpl implements UserRoleService {

    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    public UserRoleMapper getMapper() {
        return userRoleMapper;
    }

    @Override
    public void create(UserRoleDTO dto) {
        deleteByUserId(dto.getUserId());
        List<Long> roleIds = dto.getRoleIds();
        if(roleIds != null && roleIds.size() > 0){
            for (Long roleId:roleIds) {
                UserRole userRole=new UserRole();
                userRole.setUserId(dto.getUserId());
                userRole.setRoleId(roleId);
                insert(userRole);
            }
        }
    }

    @Override
    public void deleteByUserId(Long userId) {
        getMapper().deleteWithLogicByUserId(userId);
    }

    @Override
    public void deleteByRoleId(Long roleId) {
        getMapper().deleteWithLogicByRoleId(roleId);
    }

}